﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;
using MarginTrading.Entity;
using MarginTrading.Services;



namespace MarginTrading.Forms
{
    public partial class MonthlyInterest : Form
    {
        public MonthlyInterest()
        {
            InitializeComponent();
        }

        private void ProcessBtn_Click(object sender, EventArgs e)
        {
            Dictionary<String, double> InterestMap = null;
            try
            {
                InterestMap = DailyInterestServices.GetCumInterest(dateTimePickerFrom.Value,
                            dateTimePickerTo.Value);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Database Error", "Error");
            }

            if (InterestMap == null || InterestMap.Count == 0)
            {
                MessageBox.Show("No records to update","Message.");
                return;    
            }
            int count = 0;
            progressBar1.Value = 0;
            progressBar1.Maximum = InterestMap.Count;

            foreach (KeyValuePair<String, double> entry in InterestMap)
            {
                count++;
                LoanAccount LoanAcc = new LoanAccount();
                LoanAcc.Amount = entry.Value;
                LoanAcc.ClientCode = entry.Key;
                LoanAcc.EntryType = "IN";
                LoanAcc.CreatedDate = DateTime.Now;
                LoanAcc.Remarks = "Interest("+ dateTimePickerFrom.Value.ToShortDateString() +
                                  "-"+ dateTimePickerTo.Value.ToShortDateString() + ")";
                LoanAcc.TrParticulars = "Interest(" + dateTimePickerFrom.Value.ToShortDateString() +
                                        "-" + dateTimePickerTo.Value.ToShortDateString() + ")";
                LoanAcc.ReferenceNo = "IN-"+DateTime.Now.ToString("yyyyMMddHHmmss")+ "-" + count;
                try
                {
                    DailyInterestServices.UpdateStatus(entry.Key, dateTimePickerFrom.Value.Date
                        , dateTimePickerTo.Value.Date, 1);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Database Error", "Error");
                    return;
                }
                
                try
                {
                    LoanAccountServices.Add(LoanAcc);
                    progressBar1.Increment(1);
                }
             
                catch (System.Exception ex)
                {                    
                    MessageBox.Show("Database Error", "Error");
                }
                

            }

        }
    }
}
